<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>

    <style type="text/css">
        body, html, #allmap {
            width: 100%;
            height: 600px;
            overflow: hidden;
            margin: 0;
            font-family: "微软雅黑";
        }
    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=hNjkLm60gIgCRDT3kxjN4LHFlD4pxdgk"></script>

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>首页
            <small>公司地址管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>公司地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <template>
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="分布图" name="first">
                    <div id="allmap"></div>
                </el-tab-pane>

                <el-tab-pane label="详细信息" name="second">
                    <div class="box">
                        <div class="filter-container">
                            <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                                      class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                            <el-button @click="searchBut()" class="dalfBut">查询</el-button>
                            <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        </div>
                        <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                            <el-table-column type="index" align="center" label="序号"></el-table-column>
                            <el-table-column prop="addressname" label="公司地址" align="center"></el-table-column>
                            <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
                            <el-table-column prop="latitude" label="所在纬度" align="center"></el-table-column>
                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination-container">
                            <el-pagination
                                    class="pagiantion"
                                    @current-change="handleCurrentChange"
                                    :current-page="pagination.currentPage"
                                    :page-size="pagination.pageSize"
                                    layout="total, prev, pager, next, jumper"
                                    :total="pagination.total">
                            </el-pagination>
                        </div>

                        <!-- 新增标签弹层 -->
                        <div class="add-form">
                            <el-dialog title="新增地址" :visible.sync="dialogFormVisible">
                                <div class="filter-container">
                                    <el-input placeholder="搜索地址" v-model="pagination.queryString" style="width: 350px;"
                                              class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                                    <el-button type="primary" class="butT" @click="handleCreate()">确定添加</el-button>
                                </div>
                            </el-dialog>
                        </div>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </template>


    </div>
</div>
</body>
<script type="text/javascript">

    var vue = new Vue({
        el: "#app",
        data: {
            activeName: 'first',
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 8,
                total: 20,
                queryString: null,
            },
            dataList: [
               /* {
                    id: 1,
                    addressname: "武汉大学",
                    longitude: 114.360734,
                    latitude: 30.541093,
                },
                {
                    id: 1,
                    addressname: "华中科技大学",
                    longitude: 114.360734,
                    latitude: 30.541093,
                }*/
            ],//列表数据
            formData:{},
            dialogFormVisible:false//控制添加窗口显示/隐藏
        },
        //钩子函数
        mounted() {
            this.initMap();
        },
        methods: {
            //初始化地图
            initMap() {
                // 百度地图API功能
                var map = new BMap.Map("allmap");    // 创建Map实例
                map.centerAndZoom(new BMap.Point(114.30, 30.60), 11);  // 初始化地图,设置中心点坐标和地图级别
                map.setCurrentCity("武汉");          // 设置地图显示的城市 此项是必须设置的
                map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

                this.geocodeSearch(map);
            },
            //添加标注
            geocodeSearch(map) {
                //发送异步请求,获取所有的地址
                axios.post("/address/findAll.do").then((res) => {

                    if (res.data.flag){//请求成功
                        for (let i = 0; i < res.data.data.length; i++) {
                            // 创建地址解析器实例
                            var myGeo = new BMap.Geocoder();
                            // 将地址解析结果显示在地图上,并调整地图视野
                            let addressname = res.data.data[i].addressname;
                            myGeo.getPoint(addressname, function (point) {
                                if (point) {
                                    map.centerAndZoom(point, 10);
                                    var address = new BMap.Point(point.lng, point.lat);
                                    var marker = new BMap.Marker(address);
                                    map.addOverlay(marker);
                                    marker.setLabel(new BMap.Label((i+ 1)+":"+addressname,{offset:new BMap.Size(20,-10)}));
                                }
                            }, "武汉市");
                        }
                    } else {//请求失败
                        this.$message.error(res.data.message);
                    }
                });
            },
            //切换标签页
            handleClick(tab, event) {
                console.log(tab.name);
                if (tab.name === "second") {
                    this.findPage();
                }else {
                 location.href="address.html";
                }
            },
            //详细信息页面_分页查询
            findPage() {
                //封装请求参数
                var params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送ajax请求
                axios.post("/address/findPage.do", params).then((res) => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                });
            },
            //详细信息页面_查询按钮
            searchBut(){
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //详细信息页面_切换页码
            handleCurrentChange(current) {
                this.pagination.currentPage=current;
            },
            //详细信息页面_删除按钮
            handleDelete(row){
                this.$confirm('是否删除该地址?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送ajax请求
                    axios.post("/address/deleteById.do?id="+row.id).then((res)=>{
                        if (res.data.flag){//操作成功
                            this.$message({
                                message:res.data.message,
                                type:"success"
                            });
                        } else {//操作失败
                            this.$message.error(res.data.message);
                        }
                    }).finally(()=>{
                        this.pagination.currentPage = 1;
                        this.findPage();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //详细信息页面_弹出新增窗口
            handleCreate(){
                this.dialogFormVisible=true;
            }
        }
    })

</script>
</html>
